//
//  NotiDetailView.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/5.
//

import Foundation
import UIKit

class NotiDetailView: UIScrollView {
    let avaterIV = UIImageView()
    let nameL = UILabel()
    let dateL = UILabel()
    let titleL = UILabel()
    let contentL = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        showsVerticalScrollIndicator = false
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reloadData(withModal modal: NoticeModal?) {
        avaterIV.image = UIImage(named: "ic_default_av")
        nameL.text = modal?.createBy
        dateL.text = modal?.createTime
        titleL.text = modal?.noticeTitle
        
        let attri = modal?.noticeContent?.html2AttributedString
        
        if attri != nil {
            let pstyle = NSMutableParagraphStyle()
            pstyle.lineSpacing = 6
            let mAttri = NSMutableAttributedString(attributedString: attri!)
            mAttri.addAttributes([.paragraphStyle : pstyle], range: NSRange(location: 0, length: attri!.string.count))
            contentL.attributedText = mAttri
        } else {
            let txt = modal?.noticeContent ?? ""
            let pstyle = NSMutableParagraphStyle()
            pstyle.lineSpacing = 6
            contentL.attributedText = NSAttributedString(string: txt, attributes: [.paragraphStyle : pstyle])
        }
    }
    
    func setupUI() {
        avaterIV.contentMode = .scaleAspectFill
        avaterIV.layer.masksToBounds = true
        avaterIV.layer.cornerRadius = 20
        addSubview(avaterIV)
        avaterIV.snp.makeConstraints { make in
            make.top.equalTo(self.snp.top).offset(17)
            make.left.equalTo(self.snp.left).offset(20)
            make.width.height.equalTo(40)
        }
        
        nameL.textColor = .hex("#333333")
        nameL.font = .systemFont(ofSize: 17)
        addSubview(nameL)
        nameL.snp.makeConstraints { make in
            make.top.equalTo(avaterIV.snp.top)
            make.left.equalTo(avaterIV.snp.right).offset(12)
        }
        
        dateL.textColor = .hex("#C8C8C8")
        dateL.font = .systemFont(ofSize: 12)
        addSubview(dateL)
        dateL.snp.makeConstraints { make in
            make.bottom.equalTo(avaterIV.snp.bottom).offset(-2)
            make.left.equalTo(nameL.snp.left)
        }
        
        titleL.font = .systemFont(ofSize: 17)
        addSubview(titleL)
        titleL.snp.makeConstraints { make in
            make.top.equalTo(avaterIV.snp.bottom).offset(27)
            make.left.equalTo(avaterIV.snp.left)
            make.right.equalTo(self.snp.right).offset(-20)
            make.width.equalTo(ScreenWidth - 40)
        }
        
        contentL.textColor = .hex("#444444")
        contentL.font = .systemFont(ofSize: 15)
        contentL.numberOfLines = 0
        addSubview(contentL)
        contentL.snp.makeConstraints { make in
            make.top.equalTo(titleL.snp.bottom).offset(22)
            make.left.equalTo(avaterIV.snp.left)
            make.right.equalTo(titleL.snp.right)
            make.width.equalTo(ScreenWidth - 40)
        }
        
        let fjV = NotiFJView()
        fjV.isHidden = true
        addSubview(fjV)
        fjV.snp.makeConstraints { make in
            make.top.equalTo(contentL.snp.bottom).offset(35)
            make.left.equalTo(contentL.snp.left)
            make.right.equalTo(contentL.snp.right)
            make.width.equalTo(ScreenWidth - 40)
            make.height.equalTo(80)
            make.bottom.equalTo(self.snp.bottom).offset(-20)
        }
    }
}


class NotiFJView: UIView {
    let iv = UIImageView()
    let keyL = UILabel()
    let valL = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .bg
        layer.cornerRadius = 10
        layer.masksToBounds = true
        
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        iv.image = UIImage(named: "ic_fj")
        iv.contentMode = .scaleAspectFit
        addSubview(iv)
        iv.snp.makeConstraints { make in
            make.top.equalTo(self.snp.top).offset(19)
            make.left.equalTo(self.snp.left).offset(20)
            make.width.equalTo(12)
            make.height.equalTo(14)
        }
        
        keyL.text = "附件"
        keyL.textColor = .hex("#333333")
        keyL.font = .systemFont(ofSize: 16)
        addSubview(keyL)
        keyL.snp.makeConstraints { make in
            make.centerY.equalTo(iv.snp.centerY)
            make.left.equalTo(iv.snp.right).offset(8)
        }
        
        valL.text = "暂无附件"
        valL.textColor = .hex("#C8C8C8")
        valL.font = .systemFont(ofSize: 15)
        addSubview(valL)
        valL.snp.makeConstraints { make in
            make.top.equalTo(keyL.snp.bottom).offset(12)
            make.left.equalTo(keyL.snp.left)
        }
    }
}
